﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ExplorerPlus.DomainObjects;

namespace ExplorerPlus.PersistenceLayer.Sql
{
    internal static class Common
    {
        public static List<FlyWeightItem> GetFlyWeightItems(string sqlCommand, string id, string name)
        {
            List<FlyWeightItem> items = new List<FlyWeightItem>();

            // The ExecuteReader call will request the connection to be closed upon
            // the closing of the DataReader. The DataReader will be closed 
            // automatically when it is disposed.
            using (IDataReader dataReader = ExecuteDataReader(sqlCommand))
            {
                // Iterate through DataReader and put results to collection.
                while (dataReader.Read())
                {
                    // Get the value of the 'Name' column in the DataReader
                    FlyWeightItem item = new FlyWeightItem();
                    item.Id = Convert.ToInt32(dataReader[id]);
                    item.Name = dataReader[name].ToString();
                    items.Add(item);
                }

                dataReader.Close();
            }

            return items;
        }

        public static IDataReader ExecuteDataReader(string sqlCommand)
        {
            // DataReader that will hold the returned results		
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            // The ExecuteReader call will request the connection to be closed upon
            // the closing of the DataReader. The DataReader will be closed 
            // automatically when it is disposed.
            return db.ExecuteReader(dbCommand);
        }

        public static bool ExecuteNonQuery(string sqlCommand)
        {
            // DataReader that will hold the returned results		
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            return (dbCommand.ExecuteNonQuery() > 0);
        }

        public static object ExecuteScalar(string sqlCommand)
        {
            // DataReader that will hold the returned results		
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

            return dbCommand.ExecuteScalar();
        }
    }
}
